<?php
/**
 * APP版本号
 */

namespace App\OpenApi\AdminApi\Sys;

use App\Library\Service\Upload;
use App\Service\Core\Sys\Config\AppVersionListService;
use App\Service\Core\Sys\Config\AppVersionService;
use App\Service\Core\Sys\Config\AppVersionStoreService;
use App\Service\OpenApi\AdminApi\OpenBaseApi;

class AppVersionApi extends OpenBaseApi
{
    /**
     * APP版本号列表
     * @return array
     */
    public function index(): array
    {
        $list_res = (new AppVersionListService())
            ->setFields(['*', 'app_file_url'])
            ->setAppPlatform($this->input('app_platform'))
            ->setTargetVersion($this->input('target_version'))
            ->setMinVersion($this->input('min_version'))
            ->setPage($this->input('page', 1), $this->input('pageSize', $this->page_size))
            ->getList();
        return alert_info(0, 'ok', $list_res);
    }

    /**
     * 保存/修改
     * @return array
     */
    public function store(): array
    {
        set_time_limit(0);
        $id = (int)$this->input('id');
        $app_file = $this->input('app_file');

        if ($id <= 0) {
            if (empty($app_file)) {
                return alert_info(1, '请上传文件');
            }
        } else {
            $AppVersionService = AppVersionService::getInstance();
            $old_info = $AppVersionService->getOne($id);
            if (empty($old_info)) {
                return alert_info(1, '记录不存在');
            }
        }
        return (new AppVersionStoreService())
            ->setOperate($this->operate_id, $this->operate_by)
            ->setAppPlatform($this->input('app_platform'))
            ->setTargetVersion($this->input('target_version'))
            ->setMinVersion($this->input('min_version'))
            ->setAppFile($app_file)
            ->setAppIntro($this->input('app_intro'))
            ->store($id);
    }

    /**
     * 删除一个
     * @return array
     */
    public function delOne(): array
    {
        return AppVersionService::getInstance()->delOne($this->input('id'), $this->operate_id, $this->operate_by);
    }

    /**
     * 获取一个信息
     * @return array
     */
    public function info():array
    {
        $version_info = AppVersionService::getInstance()->getOne($this->input('id'));
        return alert_info(0, 'ok', $version_info);
    }
}
